package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.wemedia.pojos.WmNews;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

@Component
public class ArticleDownListener {
	@Autowired
	private ApArticleConfigMapper apArticleConfigMapper;
	
	@KafkaListener(topics = "news.down.up.topic")
	public void consumer(String message) {
		if (StringUtils.isEmpty(message)) {
			return;
		}
		
		WmNews wmNews = JSON.parseObject(message, WmNews.class);
		if (wmNews == null) {
			return;
		}
		
		Long articleId = wmNews.getArticleId();
		ApArticleConfig apArticleConfig = apArticleConfigMapper.selectById(articleId);
		
		if (apArticleConfig == null) {
			return;
		}
		
		Short enable = wmNews.getEnable();
		//文章管理的上下架和
		apArticleConfig.setIsDown(enable == 0);
		apArticleConfigMapper.updateById(apArticleConfig);
		
	}
}
